//
//  ReversiBoardPosition.m
//  Reversi
//
//  Created by 成瀬 龍人 on 2/5/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ReversiBoardPosition.h"

@implementation ReversiBoardPosition
@synthesize x,y;
- (id)init:(ReversiBoardPosition*)pos{
	[super init];
	x = pos.x;
	y = pos.y;
	return self;
}

- (id)initWithInt:(int)aX :(int)aY{
	[super init];
	if ((1 <= aX && aX <= 8) &&
		(1 <= aY && aY <= 8)){
		x = aX;
		y = aY;
	}else {
		x = 0;
		y = 0;
		@throw 	[[ReversiOutOfRangeException alloc] initWithInt:aX:aY];
	}
	return self;
}

- (id)initWithString:(NSString*)aX :(NSString*)aY{
	[super init];
	if ([aX length] != 1 || [aY length] != 1) {
		@throw [[ReversiOutOfRangeException alloc] initWithString:aX:aY];
	}else {
		unichar xChar = [aX characterAtIndex:0];
		unichar yChar = [aY characterAtIndex:0];
		if ('1' <= xChar && xChar <= '8'){
			x = xChar - '1' + 1;
		}else if ('a' <= xChar && xChar <= 'h'){
			x = xChar - 'a' + 1;
		}else {
			@throw [[ReversiOutOfRangeException alloc] initWithString:aX:aY];
		}
		if ('1' <= yChar && yChar <= '8'){
			y = yChar - '1' + 1;
		}else if ('a' <= yChar && yChar <= 'h'){
			y = yChar - 'a' + 1;
		}else {
			@throw [[ReversiOutOfRangeException alloc] initWithString:aX:aY];
		}
	}
	return self;
}

- (bool)isEqual:(id)object{
	if ([object isKindOfClass:[self class]]){
		return [object x] == x && [object y] == y;
	}
	return false;
}

- (id)copyWithZone:(NSZone*)zone{
	return [[[self class] alloc] initWithInt:x:y];
}
@end
